package pe.bf.extranet.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pe.bf.domain.bean.Empresa;
import pe.bf.domain.bean.Usuario;

/**
 * Servlet implementation class servlet
 */
 
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request,response);
	}
	
	public boolean validarIntentos(HttpServletRequest re){
		Integer intento=0;
		if(re.getSession().getAttribute("intento")==null){
			 re.getSession().setAttribute("intento",1);
		}else{
			intento=(Integer)re.getSession().getAttribute("intento");
			re.getSession().setAttribute("intento",intento+1);
		}
		
		if(intento+1==3){
			return true;
		}
		return false;
	}
	
	public void iniciarSesion(HttpServletRequest re){
		if(re.getSession().getAttribute("pos1")==null){
			HttpSession session=re.getSession(true);
		}
	}

	public void generacionDeTeclado( HttpServletRequest request ){
		
		Random random=new Random();
		ArrayList pos=new ArrayList();
		int indice=0;
		boolean ramdonBol=false;
		while (!ramdonBol){
			if(indice==0 ){
				pos.add(random.nextInt(10));
				indice++;
			}else{
				Integer valor=random.nextInt(10);
				if(!pos.contains(valor)){
					pos.add(valor);
					indice++;
				}
			}
			 
			if(indice==10){
				ramdonBol=true;
			}
		} 
		request.getSession().setAttribute("pos1", pos.get(0));
		request.getSession().setAttribute("pos2", pos.get(1));
		request.getSession().setAttribute("pos3", pos.get(2));
		request.getSession().setAttribute("pos4", pos.get(3));
		request.getSession().setAttribute("pos5", pos.get(4));
		request.getSession().setAttribute("pos6", pos.get(5));
		request.getSession().setAttribute("pos7", pos.get(6));
		request.getSession().setAttribute("pos8", pos.get(7));
		request.getSession().setAttribute("pos9", pos.get(8));
		request.getSession().setAttribute("pos10", pos.get(9));
	}
	
	
	public boolean verificarUsuario(Usuario usuario){
		
		return true;
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 		// TODO Auto-generated method stub
		iniciarSesion(request);
		if(request.getParameter("accion")!=null){ 
			String txtRuc=request.getParameter("txtRuc");
			String txtUsu=request.getParameter("txtUsu");
			String txtCla=request.getParameter("txtCla");
			
			Usuario usuario=new Usuario();
			usuario.setEmpresa(new Empresa(txtRuc));
			usuario.setNrodocumento(txtUsu);
			usuario.setClave(txtCla);
			
			if(verificarUsuario(usuario)){
				
				request.getSession().setAttribute("usuarioBean",usuario);
				request.getSession().removeAttribute("error"); 
				request.getRequestDispatcher("usuarioServlet").forward(request, response); 
				
			}else{
				
				if(validarIntentos(request)){
					//bloqueo de clave
					request.getSession().setAttribute("error","Supero los intentos permitidos. Clave bloqueada");
				}else{
					request.getSession().setAttribute("error","Clave ingresada incorrecta.");
					generacionDeTeclado(request);
				}
				request.getRequestDispatcher("index.jsp").forward(request, response); 
			}	
		}else{ 
			generacionDeTeclado(request);
			request.getRequestDispatcher("index.jsp").forward(request, response); 
		}
		
	}

}
